<?php

/**
 * 作者：lucas
 * 链接：
 * 邮箱：dissipator_520@qq.com
 */

/**
 * NodeAction
 *
 * @author lucas
 */
class ImportAction extends BaseAction {

    public function index() {
        $this->display();
    }

    public function getData() {

        $Mode = D('ImportView');
        $data = $Mode->where($this->condition)->page($this->page, $this->rows)->order($this->order)->select();
        Log::write($Mode->getLastSql());
        $this->returnGridData($data, $Mode->where($this->condition)->count());

    }

    public function toAdd() {

        // p($_post);
// <<<<<<< HEAD

// =======
// >>>>>>> 421d9175a2767bf3e4fa121f5eb5a1e98b092ce8
        $fields=array('Custml','Mdate','序号', 'trackno','Itemno','Item','Weight','QTY','amount', 'inv', 'Rdate','Idate','Rqty','bgdh', '备注');
        foreach ($fields as $key => $value) {
                    switch ($value)
                    {
                    case 'Custml':
                        $output .= '
                        <tr>
                            <td>' .$value .':</td>
                            <td valign="middle" colspan="2">
                            <input id="mm" class="easyui-combobox" name="'.$value.'" style="width:150px;"  
                                    data-options="
                                    valueField:\'手册名称\',
                                    textField:\'手册名称\',
                                    url:\'__ROOT__/import/getmData\'
                                    " />  
                            </td>
                        </tr>';
                        break;
                    case 'Itemno':
                        $output .= '
                        <tr>
                            <td>' .$value .':</td>
                            <td valign="middle" colspan="3">
// <<<<<<< HEAD
                            <input id="c" class="easyui-combogrid" name="' .$value .'" style="width:150px;"
                                    data-options="
                                                panelWidth:300,
                                                value:\'4\',
                                                idField:\'id\',
                                                textField:\'id\',
                                                url:\'__ROOT__/import/comboData\',
                                                columns:[[
                                                    {field:\'id\',title:\'id\',width:60},
                                                    {field:\'name\',title:\'Name\',width:100},
                                                    {field:\'descp\',title:\'描述\',width:100}
                                                ]],
                                                onSelect: function(rec,v){;
                                                    $(\'#ccc\').textbox(\'setValue\',v.name);
                                                    $(\'#ccc\').textbox(\'setText\',v.name);
					console.log(v.name);
                                                }
                            "/>
// =======
                            <input id="cc" class="easyui-comboGrid" name="' .$value .'" style="width:150px;"/>
// >>>>>>> bb93f574d2920d1c8e97b349ba3546334497cbb6
                            </td>
                        </tr>';
                        break; 
                     case 'Item':
                        $output .= '<tr><td>' .$value .'：</td>
                        <td>
// <<<<<<< HEAD
                        <input id="ccc" name="' .$value .'" class="easyui-textbox" style="width: 150px;"  />
// =======
                        <input id="cc2" name="' .$value .'"  class="easyui-combogrid"  style="width: 150px;" value="" data-options="
                                    valueField:\'name\',
                                    textField:\'name\',
                                    columns:[[    
                                            {field:\'name\',title:\'Name\',width:100},
                                        ]]
                                    "  />  
// >>>>>>> bb93f574d2920d1c8e97b349ba3546334497cbb6
                        </td></tr>';
                        break;
                     case 'Custml':
                        $output .= '<tr><td>' .$value .'：</td><td><input name="'.$value.'"required="required" type="text" value="C79013350240" style="width: 150px;"/></td></tr>';
                        break;
                     case 'Mdate':
                        $output .= '<tr><td>' .$value .'：</td><td><input name="'.$value.'"class="easyui-datebox"  required="required" type="text" value="" style="width: 150px;"/></td></tr>';
                      break;
                    default:
// <<<<<<< HEAD
                        $output .= '<tr><td>' .$value .'：</td><td><input class="easyui-textbox" name="'.$value.'"  value="" style="width: 150px;"/></td></tr>';
// =======
                        $output .= '<tr><td>' .$value .'：</td><td><input name="'.$value.'"class="easyui-textbox" value="" style="width: 150px;"/></td></tr>';
// >>>>>>> bb93f574d2920d1c8e97b349ba3546334497cbb6
                    }

            $data['output']= $output;

            // echo  $output;
            $this->assign('data',$data);
        }
        $this->display('add');
    }



    public function doSave2() {
        // p($_POST);
        // die;
        $Model = M("import");

        if (!$Model->create()) {
            $this->returnStatus(false, $Model->getError());
        } else {
            if (empty($Model->ID)) {
                $Model->add();
            } else {
                $Model->save();
            }

            if ($Model->getError()) {
                $this->returnStatus(false, $Model->getError());
            }
            $this->returnStatus();
        }
        // p ($_POST);
    }


    public function doSave() {

        $Model = M("import");
        $data=$_POST;
        // p($_POST);
        // die;
        if ($data['ID']){
            $Model -> data($data) -> where('ID=%d',$data['ID']) -> save();
                    // Log::write($Mode->getLastSql());
            $result = true;
        }else{ 
                if (!$Model->create()) {
                    $this->returnStatus(false, $Model->getError());
                } else {
                    $Model->startTrans();
                    $result = false;
                    if (empty($Model->ID)) {
                        $result = $Model->data($data)->add();
                        // p($data);
                    }
        }}       
            $Model->commit();
            $this->returnStatus();
    }

    public function doDelete() {
        $ids = $_POST['ids'];
        $array = explode(',', $ids);
        // p($array);
        if ($array && count($array) > 0) {
            $Model = M("import");
            foreach ($array as $id) {
                $Model->where('ID = %d', $id)->delete();
            }
        }
        $this->returnStatus();
    }

    public function toUpdate() {
        $ID = $_GET['ID'];

        $fields=array('Custml','Mdate','序号', 'trackno','Itemno','Item','Weight','QTY','amount', 'inv', 'Rdate','Idate','Rqty','bgdh', '备注','ID');
        if ($ID) {
            // $Model = M("import");
            // $import = $Model->field('ID,saname,name')->select();
            // $this->assign('import', json_encode($import));
            $Model = M('import');
            $data = $Model->where('ID = %d', $ID)->field($fields)->find();
            // p($data);
            if ($data) {
                foreach ($data as $key => $value) {
                    switch ($key)
                    {
                    case 'ID':
                        $output .= '<input name="ID" required="required" type="hidden" value="' .$value. '" style="width:150px;"/>';
                      break;  
                    case 'Custml':
                        $output .= '
                        <tr>
                            <td>' .$value .':</td>
                            <td valign="middle" colspan="3">
<<<<<<< HEAD
                            <input id="mm" class="easyui-combobox" name="'.$key.'" value="' .$value. '" style="width:150px;" data-options="valueField:\'手册名称\',textField:\'手册名称\',url:\'__ROOT__/import/getmData\'" />  
=======
                            <input id="mm" class="easyui-combobox" name="'.$key.'" value="' .$value. '" data-options="valueField:\'手册名称\',textField:\'手册名称\',url:\'__ROOT__/import/getmData\'" style="width: 150px;"/>  
>>>>>>> bb93f574d2920d1c8e97b349ba3546334497cbb6
                            </td>
                        </tr>';
                        break; 
                    case 'Itemno':
                        $output .= '
                        <tr>
                            <td>' .$key .':</td>
                            <td valign="middle" colspan="3">
                            <input id="cc" class="easyui-combogrid" name="' .$key .'" style="width:150px;"
                                    data-options="
                                                panelWidth:300,
                                                value:\'' .$value. '\',
                                                idField:\'id\',
                                                textField:\'id\',
                                                url:\'__ROOT__/import/comboData\',
                                                columns:[[
                                                    {field:\'id\',title:\'id\',width:60},
                                                    {field:\'name\',title:\'Name\',width:100},
                                                    {field:\'descp\',title:\'描述\',width:100}
                                                ]],
                                                onSelect: function(rec,v){;
                                                    $(\'#ccc\').textbox(\'setValue\',v.name);
                                                    $(\'#ccc\').textbox(\'setText\',v.name);
                                                    console.log(v.name);
                                                }
                            "/>
                            </td>
                        </tr>';
                        break;
                    case 'Item':
                        $output .= '<tr><td>' .$key .'：</td>
                        <td>
<<<<<<< HEAD
                        <input id="ccc" name="' .$key .'" class="easyui-textbox" style="width: 150px;"  />
=======
                        <input id="cc2" name="' .$key .'"  class="easyui-combogrid"  style="width: 150px;"  data-options="
                                    value:\''.$value. '\',
                                    valueField:\'name\',
                                    textField:\'name\',
                                    columns:[[    
                                            {field:\'name\',title:\'Name\',width:100},
                                        ]]
                                    "  />  
>>>>>>> bb93f574d2920d1c8e97b349ba3546334497cbb6
                        </td></tr>';
                        break;
                    case 'Mdate':
                        $output .= '<tr><td>' .$key .'：</td><td><input name="'.$key.'"class="easyui-datebox"  required="required" type="text" value="' .$value .'" style="width: 150px;"/></td></tr>';
                        break;
                    case 'Rdate':
                        $output .= '<tr><td>' .$key .'：</td><td><input name="'.$key.'"class="easyui-datebox"  required="required" type="text" value="' .$value .'" style="width: 150px;"/></td></tr>';
                        break;
                    case 'Idate':
                        $output .= '<tr><td>' .$key .'：</td><td><input name="'.$key.'"class="easyui-datebox"  required="required" type="text" value="' .$value .'" style="width: 150px;"/></td></tr>';
                        break;
                    default:
// <<<<<<< HEAD
                        $output .= '<tr><td>' .$key .'：</td><td><input name="'.$key.'"  class="easyui-textbox" value="' .$value. '" style="width: 150px;"/></td></tr>';
// =======
                    $output .= '<tr><td>' .$key .'：</td><td><input name="'.$key.'" class="easyui-textbox" value="' .$value. '" style="width: 150px;"/></td></tr>';
// >>>>>>> bb93f574d2920d1c8e97b349ba3546334497cbb6
                    }
                    $data['output']=$output ;
                }
                $this->assign('data', $data);
            }
            else 
            $this->assign('data', '无效ID号请检查！');

        }
        $this->display('add');
    }

    /**
     * 获取combo数据
     */
    public function comboData($id='') {
        $scitem = M('scitem');

        if($id){
            // return 1;
        $data = $scitem->where('ID=%d and importid is not null',$id)->select();
        $counts= $scitem->where('ID=%d and importid is not null',$id)->count();
        }
        else{
            // return 2;
            $data = $scitem->where('importid is not null')->select();
            $counts= $scitem->where('importid is not null')->count();
        }
        $this -> returnGridData($data,$counts);
        // $this->ajaxReturn(genTree($data,'id','name'));
    }

    public function getmData() {
        $Mode = M('costums_sc');
        $data = $Mode->where($this->condition)->page($this->page, $this->rows)->order('id desc')->select();
        Log::write($Mode->getLastSql());
        // $this->returnComboData($data, $Mode->where($this->condition)->count());
        $this->ajaxReturn(genTree($data,'手册名称'));
    }

}
